local extension = Package:new("YS__card", Package.CardPack)
extension.extensionName = "2333333"

Fk:loadTranslationTable{
  ["YS__card"] = "○衍生牌",
}

local boomttbj = fk.CreateTriggerSkill{
  name = "#boomttbj",
  attached_equip = "ttbj",
  frequency = Skill.Compulsory,
}
Fk:addSkill(boomttbj)
local ttbj = fk.CreateWeapon{
    name = "&ttbj",
    suit = Card.Heart,
    number = 2,
    attack_range = 0,
    equip_skill = boomttbj,
    on_uninstall = function(self, room, player)
      Weapon.onUninstall(self, room, player)
      if not player.dead then
      room:damage{
        to = player,
        damage = 1,
        damageType = fk.FireDamage,
        skillName = self.name,
      }
      end
    end,
  }
  extension:addCard(ttbj)
  extension:addCards({
    ttbj:clone(Card.Heart, 4),
    ttbj:clone(Card.Heart, 6),
    ttbj:clone(Card.Heart, 8),
    ttbj:clone(Card.Diamond, 3),
    ttbj:clone(Card.Diamond, 5),
    ttbj:clone(Card.Diamond, 7),
    ttbj:clone(Card.Diamond, 9),
  })
  Fk:loadTranslationTable{
    ["ttbj"] = "兔兔伯爵",
    [":ttbj"] = "装备牌·武器<br /><b>攻击范围</b>：0<br /><b>武器技能</b>：锁定技，此牌离开你的装备区时，你受到一点火焰伤害。",
  }

  local ttbjArmorTrigger = fk.CreateTriggerSkill{
    name = "#ttbjArmorTrigger",
    global = true,
    events = {fk.DamageInflicted},
    anim_type = "negative",
    priority = 0.1,
    frequency = Skill.Compulsory,
    can_trigger = function(self, event, target, player, data)
      return target == player and player:getEquipment(Card.SubtypeArmor) and table.find(player:getEquipments(Card.SubtypeArmor), function(cid) return Fk:getCardById(cid).name == "ttbjArmor" end)
      and data.damageType == fk.FireDamage
    end,
    on_use = function(self, event, target, player, data)
      local cards = player:getEquipments(Card.SubtypeArmor)
      cards = table.filter(cards, function(cid) return Fk:getCardById(cid).name == "ttbjArmor" end)
      player.room:throwCard(cards, self.name, player, player)
    end,
  }
  Fk:addSkill(ttbjArmorTrigger)
  local ttbjArmor = fk.CreateArmor{
    name = "&ttbjArmor",
    suit = Card.Heart,
    number = 2,
    equip_skill = ttbjArmorTrigger,
    on_uninstall = function(self, room, player)
      Armor.onUninstall(self, room, player)
      if not player.dead then
      room:damage{
        to = player,
        damage = 1,
        damageType = fk.FireDamage,
        skillName = self.name,
      }
      end
    end,
  }
  extension:addCard(ttbjArmor)
  extension:addCards({
    ttbjArmor:clone(Card.Heart, 4),
    ttbjArmor:clone(Card.Heart, 6),
    ttbjArmor:clone(Card.Heart, 8),
    ttbjArmor:clone(Card.Diamond, 3),
    ttbjArmor:clone(Card.Diamond, 5),
    ttbjArmor:clone(Card.Diamond, 7),
    ttbjArmor:clone(Card.Diamond, 9),
  })
  Fk:loadTranslationTable{
    ["ttbjArmor"] = "兔兔伯爵",
    [":ttbjArmor"] = "装备牌·防具<br /><b>防具技能</b>：锁定技，此牌离开你的装备区时，你受到一点火焰伤害。当你受到火焰伤害时，你弃置此牌。",
    ["#ttbjArmorTrigger"] = "兔兔伯爵爆炸！",
  }

  local yuanshuiSkill = fk.CreateActiveSkill{
    name = "yuanshui_skill",
    prompt = "#yuanshui_skill",
    mod_target_filter = function(self, to_select)
      return not table.find(Fk:currentRoom().alive_players, function(p)
          return p.dying
        end)
    end,
    can_use = function(self, player, card)
      return not player:isProhibited(player, card)
    end,
    on_use = function(self, room, use)
      if not use.tos or #TargetGroup:getRealTargets(use.tos) == 0 then
        use.tos = { { use.from } }
      end
    end,
    on_effect = function(self, room, effect)
      local player = room:getPlayerById(effect.from)
      local target = room:getPlayerById(effect.to)
      if target:isWounded() and not target.dead then
        room:recover({
          who = target,
          num = 1,
          card = effect.card,
          recoverBy = player,
          skillName = self.name
        })
      end
      target:drawCards(1)
    end
  }
  local yuanshui = fk.CreateBasicCard{
    name = "&yuanshui",
    suit = Card.Spade,
    number = 1,
    skill = yuanshuiSkill,
  }
  extension:addCards({
    yuanshui,
    yuanshui:clone(Card.Spade, 2),
    yuanshui:clone(Card.Spade, 3),
    yuanshui:clone(Card.Spade, 4),
    yuanshui:clone(Card.Spade, 5),
    yuanshui:clone(Card.Spade, 6),
    yuanshui:clone(Card.Spade, 7),
  })
  Fk:loadTranslationTable{
    ["yuanshui"] = "<font color='#0072E3'>源水之滴</font>",
    ["#yuanshui_skill"] = "对自己使用，回复一点体力并摸一张牌。",
    [":yuanshui"] = "基本牌<br /><b>时机</b>：出牌阶段<br /><b>目标：</b>自己<br /><b>效果：</b>回复一点体力并摸一张牌",
  }

  local Seed_trigger = fk.CreateTriggerSkill{
    name = "Seed_trigger",
    global = true,
    priority = 0.1,
    mute = true,
    events = {fk.AfterCardsMove},
    can_trigger = function(self, event, target, player, data)
      for _, move in ipairs(data) do
        if move.extra_data and move.extra_data.Seed then
          if table.contains(move.extra_data.Seed, player.id) and not player.dead then
            return true
          end
        end
        if move.to == player.id and move.toArea == Card.PlayerHand and move.moveReason == fk.ReasonDraw then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).name == "YS__Seed" then
              return true
            end
          end
        end
      end
    end,
    on_trigger = function(self, event, target, player, data)
      local n = 0
      local ids = {}
      for _, move in ipairs(data) do
        if move.extra_data and move.extra_data.Seed then
          n = n + #table.filter(move.extra_data.Seed, function(id) return id == player.id end)
        end
        if move.to == player.id and move.toArea == Card.PlayerHand then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).name == "YS__Seed" then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end
      for i = 1, n, 1 do
        if not player.dead then
          player.room:loseHp(player, 1, "YS__Seed")
          player:drawCards(1)
        end
      end
      ids = table.filter(ids, function(id) return table.contains(player:getCardIds("h"), id) end)
      while not player.dead and #ids > 0 do
        ids = table.filter(ids, function(id) return table.contains(player:getCardIds("h"), id) end)
        if #ids == 0 then break end
        if not self:doCost(event, nil, player, ids) then
          break
        end
      end
    end,
    on_cost = function(self, event, target, player, data)
      local room = player.room
      room:setPlayerMark(player, "YS__Seed-tmp", data)
      local success, dat = room:askForUseActiveSkill(player, "YS__Seed_give", "#YS__Seed-give", true)
      room:setPlayerMark(player, "YS__Seed-tmp", 0)
      if success then
        self.cost_data = dat
        return true
      end
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local to = room:getPlayerById(self.cost_data.targets[1])
      local dummy = Fk:cloneCard("dilu")
      dummy:addSubcards(self.cost_data.cards)
      room:moveCardTo(dummy, Card.PlayerHand, to, fk.ReasonGive, "YS__Seed_give", nil, true, player.id)
    end,
  
    refresh_events = {fk.BeforeCardsMove},
    can_refresh = function(self, event, target, player, data)
      for _, move in ipairs(data) do
        if move.from == player.id then
          return true
        end
      end
    end,
    on_refresh = function(self, event, target, player, data)
      for _, move in ipairs(data) do
        if move.from == player.id and move.skillName ~= "YS__Seed_give" and move.skillName ~= "scrape_Seed" then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand and (move.moveVisible or table.contains({2, 3, 5, 7}, move.toArea)) then
              if Fk:getCardById(info.cardId).name == "YS__Seed" then
                move.extra_data = move.extra_data or {}
                local dat = move.extra_data.Seed or {}
                table.insert(dat, player.id)
                move.extra_data.Seed = dat
              end
            end
          end
        end
      end
    end,
  }
  local YS__Seed_give = fk.CreateActiveSkill{
    name = "YS__Seed_give",
    min_card_num = 1,
    target_num = 1,
    card_filter = function(self, to_select, selected)
      return table.contains(Self:getMark("YS__Seed-tmp"), to_select)
    end,
    target_filter = function(self, to_select, selected)
      return #selected == 0 and to_select ~= Self.id
    end,
  }
  Fk:addSkill(YS__Seed_give)
  Fk:addSkill(Seed_trigger)
  local esSeedSkill = fk.CreateActiveSkill{
    name = "YS__Seed_skill",
    can_use = Util.FalseFunc,
  }
  local YS__Seed = fk.CreateBasicCard{
    name = "&YS__Seed",
    skill = esSeedSkill,
  }
  Fk:addSkill(esSeedSkill)
  extension:addCards({
    YS__Seed:clone(Card.Club, 4),
  })
  Fk:loadTranslationTable{
    ["YS__Seed"] = "丰壤之核",
    [":YS__Seed"] = "基本牌<br/><b>效果</b>：①当【丰壤之核】正面向上离开你的手牌区或作为你的拼点牌亮出后，你失去1点体力并摸一张牌。②当你因摸牌获得【丰壤之核】后，"..
    "你可以将之交给一名其他角色，以此法失去【丰壤之核】时不触发失去体力效果。",
    ["YS__Seed_give"] = "",
    ["#YS__Seed-give"] = "你可以将摸到的【丰壤之核】交给其他角色（不触发失去体力效果）",
  }

  
  return extension